const fs = require('fs');
const app = require('j7/app');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const event = require('j7/event');
const config = require('j7/config');
const bchelper = require('common/bchelper');
const common = require('./common');

const airDropDate = utils.getArgv('f');
const json = utils.readJsonFromFile
(`../bin_dev/web3tools/config/airdrops/${airDropDate}.airdrop@${airDropDate}.airdrop.json`);
let seqId = 1;
const outJson = [];
json.forEach(
  (item) => {
    for (let i = 0; i < 1; ++i) {
      const outItem = {};
      outItem['address'] = bcutils.toNormalAddress(item['address']);
      if (!bcutils.isValidAddress(item['address'])) {
        console.log(item);
        throw new Error('error address');
      }
      outItem['type'] = 'hero';
      outItem['item_id'] = common.heroList[utils.randRange(0, common.heroList.length)];
      outItem['level'] = 1;
      outItem['quality'] = 2;
      outItem['token_id'] = bchelper.genAirDropTokenId(airDropDate, utils.pad(seqId, 7));
      outJson.push(outItem);
      ++seqId;
    }
  }
);
fs.writeFileSync(
  `../bin_dev/web3tools/config/airdrops/${airDropDate}.airdrop.json`,
  utils.prettyJsonEncode(outJson));
common.jsonToCsv(
  `../bin_dev/web3tools/config/airdrops/${airDropDate}.airdrop.csv`,
  outJson,
  [
    'address',
    'type',
    'item_id',
    'level',
    'quality',
    'token_id'
  ]);
